/**
 * Created by ACER on 2017/4/14.
 */
app.controller('home',function($scope,$http){
    //获取所有主页面任务信息
    $scope.homeTaskList=[];
    $scope.page=1;
    //获取主页面任务id
    $scope.searchInfo='';
    $scope.homeTaskId='';
//获取头像地址
    $scope.homeAvatarAdd='';
//获取任务内容
    $scope.homeTaskContent='';

    $scope.homeCondition = {};


        $scope.getHomeTask=function(page){

            $scope.homeCondition.page = page ? page : 1;

            // 若该加载为初始化加载，即没有传入page参数
            if (!page) {
                $scope.homeTaskList = [];
                $scope.page = 1;
            }

            $.showPreloader('请稍后');
            $http({
                method:'get',
                url:app.netPath+'/tasks/homePageTask',
                params: $scope.homeCondition
        }).then(function(res){

                if(!res.data.error){
                var homeTaskList=res.data.data ;
                    for(var i=0;i<homeTaskList.length;i++){
                        $scope.homeTaskList.push(homeTaskList[i]);
                    }

                    var infiniteScroll = $('.infinite-scroll');
                    var infiniteScrollPreloader = $('.infinite-scroll-preloader');
                    // 启用无限加载事件
                    $.attachInfiniteScroll(infiniteScroll);
                    // 启用提示符
                    infiniteScrollPreloader.show();

                    if(res.data.allPage <= $scope.homeCondition.page){
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        $.detachInfiniteScroll(infiniteScroll);
                        // 删除加载提示符
                        infiniteScrollPreloader.hide();}
                }
                else {

                    console.log(res.data.msg);
                }
                $scope.isOnloading = false;
                $.hidePreloader();
            },function(){
                $.alert('网络错误');
                $.hidePreloader();
                $scope.isOnloading = false;
            })
        };


    //得到主页面的任务ID
    $scope.getUserOtherId=function(otheruserid){
        sessionStorage.setItem('otheruserid',otheruserid);
        location.href='#others-profile';
    };

    $scope.getHomeTaskId=function(taskhomeid){
        sessionStorage.setItem('taskhomeid',taskhomeid);
        location.href='#others-task-details';
    };




    //主页面跳转到搜索界面
    $scope.turnSearch=function(){
        //得到搜索信息
        sessionStorage.setItem('searchInfo',$scope.searchInfo);
        location.href = '#search';
    };

    $scope.saveType = function(typeId){
        sessionStorage.setItem('typeid',typeId);
        $scope.turnSearch();
    }

});